﻿/*
	zealotwms 仓库管理系统
	Copyright (C) 2011  狂热者工作组 
    内容：常用函数类
    时间: 2011-9-5
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace Zwms.Client.Helper
{
    public static  class CommonHelper
    {
        public static string GetDateString(DateTime dt)
        {
            return string.Format("{0:yyyyMMdd}", dt);
        }

        public static string GetTimeString(DateTime dt)
        {
            return string.Format("{0:HHmmss}", dt);
        }

        public static DateTime GetDatetimeFromString(string sDate)
        {
            if (sDate.Length == 14)
            {
                int year = Convert.ToInt32(sDate.Substring(0, 4));
                int month = Convert.ToInt32(sDate.Substring(4, 2));
                int day = Convert.ToInt32(sDate.Substring(6, 2));
                int hour = Convert.ToInt32(sDate.Substring(8,2));
                int min = Convert.ToInt32(sDate.Substring(10, 2));
                int sec = Convert.ToInt32(sDate.Substring(12, 2));

                return new DateTime(year, month, day, hour, min, sec);
            }
            else if (sDate.Length == 8)
            {
                int year = Convert.ToInt32(sDate.Substring(0, 4));
                int month = Convert.ToInt32(sDate.Substring(4, 2));
                int day = Convert.ToInt32(sDate.Substring(6, 2));
                return new DateTime(year, month, day);
            }
            else
            {
                return new DateTime(1900, 1, 1);
            }
        }



        public static string GetDictKeyByValue(Dictionary<string, string> dict, string value)
        {
            string ret = string.Empty;
            foreach (KeyValuePair<string, string> kvp in dict)
            {
                if (value == kvp.Value)
                {
                    ret = kvp.Key;
                }
            }
            return ret;
        }

        public static T GetDataGridViewSelectRow<T>(DataGridView dataGridView) where T : class
        {
            int rowIndex = dataGridView.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                return default(T);
            }

            DataGridViewRow row = dataGridView.Rows[rowIndex];
            T dict = row.DataBoundItem as T;

            return dict;
        }

        public static void InitClass<T>(T myClass)
        {
            foreach (PropertyInfo pi in myClass.GetType().GetProperties())
            {
                if (pi.PropertyType.Name == "String"
                    && pi.GetValue(myClass, null) == null)
                {
                    pi.SetValue(myClass, "", null);

                }
            }
        }

        public static bool String2Bool(string str)
        {
            if (str == "0")
                return false;
            else
                return true;
        }

        public static String Bool2String(bool b)
        {
            if (b)
                return "1";
            else
                return "0";
        }

        public static List<string> splitStr(string mString, char str)
        {
            try
            {
                List<string> al = new List<string>();
                if (mString != "")
                {
                    string[] arr = mString.Split(str);

                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (arr[i].Trim() != "")
                        {
                            al.Add(arr[i]);
                        }
                    }
                }
                return al;
            }
            catch (Exception err)
            {
                string e = err.Message;
                return null;
            }
        }
    }
}
